/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: FileUtil.java
 * Author: ckj
 * Date: 2024/7/22 下午3:19
 * Description:
 * 文件操作
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;


/**
 * @author ckj
 */
public class FileUtil {

    private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     *
     * @author ckj
     * @date 2024/07/22 下午4:36
     * 获取文件名
     * @return java.lang.String
     */
    public static String extractFileName(String filePath) {
        Path path = Paths.get(filePath);
        return path.getFileName().toString();
    }

    public static boolean fileExist(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            File parentDir = file.getParentFile();
            if (parentDir!= null &&!parentDir.exists()) {
                if (!parentDir.mkdirs()) {
                    logger.error("创建父目录失败");
                    return false;
                }
            }
            try {
                if (!file.createNewFile()) {
                    logger.error("创建文件失败");
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
